import store from '@/store'
import { asyncRouterMap, constantRouterMap, navigationRouterMap } from '@/router'
import { usersRolesPermissions, hasUsersPermission } from '@/utils/usersPermissions'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  // if (route.meta && route.meta.role) {
  //   return roles.some(role => route.meta.role.indexOf(role) >= 0)
  // } else {
  //   return true
  // }
  var bIs = false
  if (route.meta && route.meta.role) {
    route.meta.role.map(item => {
      if (roles.some(role => role.indexOf(item) >= 0)) {
        bIs = true
      }
    })
  } else {
    bIs = false
  }
  return bIs
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(asyncRouterMap, roles) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (hasPermission(roles, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}

/**
 * 当期那项目使用的 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function newFilterAsyncRouter(asyncRouterMap, roles) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (route.children && route.children.length) {
      route.children = newFilterAsyncRouter(route.children, roles)
      if (route.children.length == 0) {
        return false;
      } else {
        return true;
      }
    } else {
      if (route.name && route.name.length > 0) {
        return roles.some(key => key === route.name)
      }
      return false;
    }
  })
  return accessedRouters
}

const permission = {
  state: {
    navigationRouters: navigationRouterMap,
    constantRouters: constantRouterMap,
    asyncRouters: asyncRouterMap,
    routers: constantRouterMap,
    addRouters: [],
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.navigationRouters = navigationRouterMap
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    BuildRoutes({ commit }, data) {
      return new Promise(resolve => {
        var permission = JSON.parse(
          localStorage.getItem("ydRobotPermissions")
        );
        let accessedRouters

        accessedRouters = newFilterAsyncRouter(asyncRouterMap, permission)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    },

    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const { roles } = data
        let accessedRouters
        // 
        var bIsAdmin = false
        if (usersRolesPermissions.admin.root) {
          usersRolesPermissions.admin.root.map(item => {
            if (roles.some(role => role.indexOf(item) >= 0)) {
              bIsAdmin = true
            }
          })
        } else {
          bIsAdmin = false
        }
        if (bIsAdmin) { // 是否最高权限
          // if (roles.indexOf(usersRolesPermissions.admin.root) >= 0) { // 是否最高权限
          accessedRouters = asyncRouterMap
        } else {
          accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        }
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
